<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Area;
use app\common\tool\Result;
use think\facade\View;
use think\exception\ValidateException;
use app\common\model\Address as AddressModel;
use think\response\Json;

class Address extends Base
{

    /**
     * get: 获取收货地址列表
     * path: index
     * method: index
     */
    public function index()
    {
        $map = ['uid' => $this->user->id, 'delete_time' => 0];
        $list = AddressModel::getList($map, false, 'create_time', ['p', 'c', 'd']);
        if(isset($list))
        {
            return Result::success('获取成功', $list);
        }else{
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 地址详情
     * path: edit
     * method: edit
     * param: id - {string} 地址id
     */
    public function edit()
    {
        $param = $this->request->param();
        $info = AddressModel::where('id', $param['id'])->find();
        $info->province_title = $info->p->title;
        $info->city_title = $info->c->title;
        $info->district_title = $info->d->title;
        if(isset($info))
        {
            return Result::success('获取成功', $info);
        }else{
            return Result::fail('获取失败');
        }
    }



    /**
     * post: 添加/修改地址
     * path: save
     * method: save
     */
    public function save()
    {
        $param = $this->request->post('', '', $this->filter);
//        try {
//            $this->validate($param, 'Address');
//        } catch (ValidateException $e) {
//            return json(['code' => -1, 'msg' => $e->getError()]);
//        }
        $add = AddressModel::add($param, $this->user);
        if($add['code']>0)
        {
            return Result::success( $add['msg']);
        }else{
            return Result::fail( $add['msg']);
        }
    }


    /**
     * post: 删除收货地址
     * path: delete
     * method: delete
     */
    public function delete()
    {
        return Result::fastReturn(AddressModel::edit($this->request->post('id'), $this->user));
    }
    // //获取省市区县Account.php
    // public function getArea($level = 0)
    // {
    //     $param = $this->request->get();
    //     $code=$param['code'];
    //     $list = Area::getArea($code ?: 100000, $level);
    //     return Result::success('获取数据成功！', $list);
    // }

    /**
     * get: 获取地址
     * path: getArea
     * method: getArea
     * param: id - {string} code
     * param: level - {string} 1省2市3区
     */
    public function getArea()
    {
        $param = $this->request->param();
        $area = Area::getArea($param['id'] ?: 100000, $param['level']);
        if(isset($area))
        {
            return Result::success('获取成功', $area);
        }else{
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 设置默认收货地址
     * path: chooseDefault
     * method: chooseDefault
     * param: id - {string} code
     * param: type - {string} 1省2市3区4街道
     */
    public function chooseDefault()
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            return Result::fastReturn(AddressModel::chooseDefault($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

    }

    /**
     * post: 选择收货地址
     * path: choice
     * method: choice
     * param: id - {string} id
     */
    public function choice()
    {
        return Result::fastReturn(AddressModel::choice($this->request->post('id'), $this->user));
    }

}

